package svc

import (
	"fmt"
	"github.com/shugen/models"
)

//Insert
func (s *Svc) InsertMenShen(ms *models.MenShen) error {

	if _, err := s.Engine.Insert(ms); err != nil {
		fmt.Println("新增失败:", ms, err)
		return err
	}
	return nil
}

//Update
func (s *Svc) UpdateMenShen(ms *models.MenShen) (*models.MenShen, error) {

	menshen := &models.MenShen{}
	//title和type不需要更新
	affect, err := s.Engine.ID(string(ms.MenShenId)).Omit("type").Omit("title").Update(ms)
	if err != nil {
		return nil, err
	}
	if affect > 0 {
		s.Engine.ID(string(ms.MenShenId)).Get(menshen)
	}
	return menshen, nil
}

//Select
func (s *Svc) SelectMenShen(ms *models.MenShen) (*models.MenShen, error) {

	menshen := &models.MenShen{}

	if _, err := s.Engine.Where("type = ?", ms.Type).Get(menshen); err != nil {
		return nil, err
	}
	return menshen, nil
}

//Select List
func (s *Svc) ListMenShen() ([]*models.MenShen, error) {

	var list []*models.MenShen

	if err := s.Engine.Find(&list); err != nil {
		return nil, err
	}
	return list, nil
}
